package com.foreknow.controller;

import com.foreknow.bean.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        //向网页响应内容
        PrintWriter out = resp.getWriter();
        //调用service中的方法查询所有留言的数据
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.getAll();
        //如何将list集合保存到一个地方,需要将List保存到服务器端,转发之后就可以再list.jsp中方便去获取
        req.setAttribute("list",list);
        //当前的Servlet中如何跳转到list.jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req,resp);














        //调用service中的方法查询所有留言的数据
//        GuestbookService service = new GuestbookServiceImpl();
//        List<Object> list = service.getAll();
//        out.println("<html>");
//        out.println("<head><title>guestbook</title></head>");
//        out.println("<body>");
//        out.println("<table>");
//        out.println("<tr>");
//        out.println("<td>id</td>");
//        out.println("<td>name</td>");
//        out.println("<td>phone</td>");
//        out.println("<td>email</td>");
//        out.println("<td>title</td>");
//        out.println("<td>content</td>");
//        out.println("<td>createdtime</td>");
//        out.println("</tr>");
//        //遍历集合list
//        for(int i = 0;i<list.size();i++){
//            Guestbook gb = (Guestbook) list.get(i);
//            out.println("<tr>");
//            out.println("<td>"+gb.getId()+"</td>");
//            out.println("<td>"+gb.getName()+"</td>");
//            out.println("<td>"+gb.getPhone()+"</td>");
//            out.println("<td>"+gb.getEmail()+"</td>");
//            out.println("<td>"+gb.getTitle()+"</td>");
//            out.println("<td>"+gb.getContent()+"</td>");
//            out.println("<td>"+gb.getCreatedtime()+"</td>");
//            out.println("</tr>");
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html>");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
